﻿#include "plan.h"
#include "ui_plan.h"

Plan::Plan(QWidget *parent) :
    BizEditor(parent),
    ui(new Ui::Plan)
{
    ui->setupUi(this);

    loadDetailColWidthFlag=false;
    table=PLAN_EPIBOLY_INDEX;
    detailTable=PLAN_EPIBOLY_DETAIL;

    model=new QSqlTableModel(this);
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    ui->indexView->setModel(model);

    detailModel=new QSqlTableModel(this);
    detailModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    ui->detailView->setModel(detailModel);

    ui->indexView->setEditTriggers(QTableView::NoEditTriggers);
    ui->detailView->setEditTriggers(QTableView::NoEditTriggers);

    if(loadColumnWidth())
    {
        loadColumnWidthFlag=true;
        loadDetailColWidthFlag=true;
    }

    updateView();
    createMenu();
}

Plan::~Plan()
{
    delete ui;
}

void Plan::updateView()
{
    updateViewUnfinish();
}

void Plan::createMenu()
{
    menu=new QMenu(this);
    generateMenu();
}

void Plan::generateMenu()
{
    menu->addAction(QStringLiteral("添加..."),this,&Plan::newItem);
    menu->addSeparator();

    menu->addAction(QStringLiteral("未完成记录"),this,&Plan::updateView);
    menu->addAction(QStringLiteral("所有记录"),this,&Plan::updateViewAll);
    menu->addSeparator();
    menu->addAction(QStringLiteral("查找..."),this,&Plan::search);
    menu->addSeparator();
    menu->addAction(QStringLiteral("【取消】"),this,&Plan::en_cancel);

    if(part != "sd")
    {
        menu->addSeparator();
        menu->addAction(QStringLiteral("进入编辑模式"),this,&Plan::edit_on);
    }
}

void Plan::search()
{
    showSearchBox("customer");
}

bool Plan::loadColumnWidth()
{
    if(!QFile::exists(userLand + ETC_BIZ))
        return false;
    else
    {
        QSettings set(userLand + ETC_BIZ,QSettings::IniFormat);
        int width=set.value("Biz/width").toInt();
        int height=set.value("Biz/height").toInt();
        if(width >= 720 && height >= 480)
            this->resize(width,height);

        colWidth.insert(2,set.value("BizColumnWidth/date").toInt());
        colWidth.insert(3,set.value("BizColumnWidth/customer").toInt());
        colWidth.insert(4,set.value("BizColumnWidth/period").toInt());
        colWidth.insert(6,set.value("BizColumnWidth/status").toInt());

        detailColWidth.insert(2,set.value("DetailColWidth/name").toInt());
        detailColWidth.insert(3,set.value("DetailColWidth/number").toInt());
        detailColWidth.insert(4,set.value("DetailColWidth/request").toInt());
        detailColWidth.insert(5,set.value("DetailColWidth/status").toInt());

        return true;
    }
}

void Plan::saveColumnWidth()
{
    QSettings set(userLand + ETC_BIZ,QSettings::IniFormat);
    set.setValue("Biz/width",this->width());
    set.setValue("Biz/height",this->height());

    set.setValue("BizColumnWidth/date",ui->indexView->columnWidth(2));
    set.setValue("BizColumnWidth/customer",ui->indexView->columnWidth(3));
    set.setValue("BizColumnWidth/period",ui->indexView->columnWidth(4));
    set.setValue("BizColumnWidth/status",ui->indexView->columnWidth(6));

    set.setValue("DetailColWidth/name",ui->detailView->columnWidth(2));
    set.setValue("DetailColWidth/number",ui->detailView->columnWidth(3));
    set.setValue("DetailColWidth/request",ui->detailView->columnWidth(4));
    set.setValue("DetailColWidth/status",ui->detailView->columnWidth(5));
}

void Plan::setHeaderData()
{
    ui->indexView->hideColumn(0);   // id
    ui->indexView->hideColumn(1);   //uid
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("日期"));    //date
    model->setHeaderData(3,Qt::Horizontal,QStringLiteral("客户"));    //customer
    model->setHeaderData(4,Qt::Horizontal,QStringLiteral("期限"));    //period
    ui->indexView->hideColumn(5);   //host
    model->setHeaderData(6,Qt::Horizontal,QStringLiteral("状态"));    //status
    model->setHeaderData(7,Qt::Horizontal,QStringLiteral("备注"));    //remark

    if(loadColumnWidthFlag)
    {
        QMap<int,int>::const_iterator itor;
        for(itor = colWidth.constBegin(); itor != colWidth.constEnd(); itor++ )
        {
            int width=itor.value();
            if(width <= 0)
                width=100;
            else if(width >= this->width())
                width =200;

            ui->indexView->setColumnWidth(itor.key(),width);
        }
    }
}

void Plan::newItem()
{
    PlanAdd bizAdd;
    bizAdd.move(this->x() + 50,this->y() + 50);
    bizAdd.exec();
    updateView();
}

void Plan::en_remove()
{
    QMessageBox::warning(this,QStringLiteral("无效操作"),QStringLiteral("计划无法删除！"));
}

void Plan::edit_on()
{
    isEdit=true;
    ui->indexView->setEditTriggers(QTableView::DoubleClicked);
    ui->detailView->setEditTriggers(QTableView::DoubleClicked);
    updateMenu();
}

void Plan::edit_off()
{
    isEdit=false;
    ui->indexView->setEditTriggers(QTableView::NoEditTriggers);
    ui->detailView->setEditTriggers(QTableView::NoEditTriggers);
    updateMenu();
}

void Plan::revert()
{
    if(ui->indexView->currentIndex().isValid())
        model->revertAll();

    if(ui->detailView->currentIndex().isValid())
        detailModel->revertAll();
}

void Plan::save()
{
    if(ui->indexView->currentIndex().isValid())
        model->submitAll();

    if(ui->detailView->currentIndex().isValid())
        detailModel->submitAll();
}

void Plan::updateDetail(const QString &uid)
{
    detailModel->setTable(detailTable);
    detailModel->setFilter(QString("uid = '%1'").arg(uid));
    detailModel->select();

    ui->detailView->hideColumn(0);  // id
    ui->detailView->hideColumn(1);  // uid
    detailModel->setHeaderData(2,Qt::Horizontal,QStringLiteral("名称"));  //name
    detailModel->setHeaderData(3,Qt::Horizontal,QStringLiteral("数量"));  //number
    detailModel->setHeaderData(4,Qt::Horizontal,QStringLiteral("要求"));  //request
    detailModel->setHeaderData(5,Qt::Horizontal,QStringLiteral("状态"));  //status
    detailModel->setHeaderData(6,Qt::Horizontal,QStringLiteral("备注"));

    if(loadDetailColWidthFlag)
    {
        QMap<int,int>::const_iterator itor;
        for(itor = detailColWidth.constBegin(); itor != detailColWidth.constEnd(); itor++ )
        {
            int width=itor.value();
            if(width <= 0)
                width=100;
            else if(width >= this->width())
                width =200;

            ui->detailView->setColumnWidth(itor.key(),width);
        }
    }
}

void Plan::en_cancel()
{
    cancelIndex();
    cancelDetail();
}

void Plan::cancelIndex()
{
    QModelIndex index=ui->indexView->currentIndex();
    if(index.isValid())
    {
        QSqlRecord record=model->record(index.row());
        int id=record.value("id").toInt();
        cancelItemDirect(table,id);
    }
}

void Plan::cancelDetail()
{
    QModelIndex index=ui->detailView->currentIndex();
    if(index.isValid())
    {
        QSqlRecord record=detailModel->record(index.row());
        int id=record.value("id").toInt();
        QString uid=record.value("uid").toString();
        cancelItemDirect(detailTable,id);
        updateDetail(uid);
    }
}

void Plan::en_finish()
{
    QModelIndex index=ui->indexView->currentIndex();
    if(index.isValid())
    {
        QModelIndex mIndex=index.sibling(index.row(),6);
        model->setData(mIndex,ST_FINISH);
    }
}

void Plan::on_indexView_clicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        QSqlRecord record=model->record(index.row());
        QString uid=record.value("uid").toString();
        updateDetail(uid);
    }
}

void Plan::on_detailView_doubleClicked(const QModelIndex &index)
{
    if(part != "td")
        return;

    if(index.isValid())
    {
        QSqlRecord record=detailModel->record(index.row());
        int detail_id=record.value("id").toInt();
        QString status = getStatus(detailTable,detail_id);
        if(status == ST_FINISH || status == ST_CANCEL)
            return;

        QString uid=record.value("uid").toString();
        QSqlQuery _query;
        _query.exec(QString("select date,customer from %1 where uid='%2'")
                    .arg(table)
                    .arg(uid));
        _query.next();

        QString date=_query.value(0).toString();
        QString customer=_query.value(1).toString();
        QString info= date + " " + customer + QStringLiteral("订单");

        BizAdd bizAdd;
        bizAdd.genRecordFromStuff(detail_id,info);
        bizAdd.move(this->x() + 50,this->y() + 50);
        if(bizAdd.exec() == QDialog::Accepted)
        {
            if(getStatus(detailTable,detail_id) != ST_RUNNING)
                changeStatus(detailTable,ST_RUNNING,detail_id);

            int index_id=getId(table,uid);
            if(getStatus(table,index_id) != ST_RUNNING)
                changeStatus(table,ST_RUNNING,index_id);

            updateView();
            updateDetail(uid);
        }
    }
}

void Plan::on_indexView_pressed(const QModelIndex &)
{
    ui->detailView->clearSelection();
    ui->detailView->setCurrentIndex(QModelIndex());
}

void Plan::on_detailView_pressed(const QModelIndex &)
{
    ui->indexView->clearSelection();
    ui->indexView->setCurrentIndex(QModelIndex());
}

void Plan::on_indexView_doubleClicked(const QModelIndex &)
{
    if(!isEdit)
        newItem();
}
